package com.lujianfei.filesend.ui.search.adapter

import androidx.databinding.BindingAdapter
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lujianfei.filesend.R
import com.lujianfei.filesend.base.BaseBindingAdapter
import com.lujianfei.filesend.databinding.ItemSearchDeviceBinding


/**
 * 扫描出来的 tag adapter
 */
class SearchDeviceAdapter(items: ObservableArrayList<SearchDeviceItem>) :
    BaseBindingAdapter<SearchDeviceItem, ItemSearchDeviceBinding>(items) {

    var onItemClick:((SearchDeviceItem)->Unit)?= null

    override fun getLayoutResId() = R.layout.item_search_device

    override fun onBindItem(binding: ItemSearchDeviceBinding?, bean: SearchDeviceItem, position: Int) {
        binding?.root?.setOnClickListener {
            onItemClick?.invoke(bean)
        }
        binding?.model = bean
    }
}

@BindingAdapter(value=["SearchDeviceAdapter_bindlist","SearchDeviceAdapter_onItemClick"], requireAll = false)
fun SearchDeviceAdapter_bindlist(view: RecyclerView,
                                 list:ObservableArrayList<SearchDeviceItem>,
                                 onItemClick:((SearchDeviceItem)->Unit)?= null,
) {
    if (view.adapter != null) {
        return
    }
    val adapter = SearchDeviceAdapter(list)
    adapter.onItemClick = onItemClick
    view.adapter = adapter
    view.layoutManager = LinearLayoutManager(view.context)
}

data class SearchDeviceItem (val ipaddress:String, val name:ObservableField<String>, val lastMessage:ObservableField<String>) {

    fun getAvatar(name:String) {

    }
}